package com.hyjx.framework.component.codetable;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

/**
 * 码表类别控制器
 */
@Controller
@RequestMapping("/codetablemanager")
public class CodeTableManagerController {
    @Resource
    private CodeTableManagerService service;

    @Resource
    private CodetableService codetableService;

    /**
     * 进入到信息查询页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = RequestMethod.GET)
    public String gotoList(HttpServletRequest request) {
        return "/codetable/list";
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList(CodeTableManagerModel model, HttpServletRequest request) {
        return service.queryList(model);
    }

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoAdd", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoAdd(HttpServletRequest request) {
        return "/codetable/add";
    }

    /**
     * 增加信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public String doAdd(CodeTableManagerModel model) {
        CodeTableManagerModel model1 = service.getInfoById(model.getCodetable());
        if (null == model1) {
            model.setOrderid("1");
            if (service.add(model)) {
                return "success";
            } else {
                return "failure";
            }
        } else {
            return "repeat";
        }
    }

    /**
     * 跳转到修改页面
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoEdit(HttpServletRequest request) {
        String id = request.getParameter("id");
        CodeTableManagerModel model = service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/codetable/edit");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 修改信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doEdit", method = RequestMethod.POST)
    @ResponseBody
    public String doEdit(CodeTableManagerModel model) {
        if (service.edit(model) > 0) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 删除信息
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoDelete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (service.delete(ids) > 0) {
            CodetableModel cm = new CodetableModel();
            cm.setCodetable(ids);
            codetableService.delete(cm);
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 信息查看
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = RequestMethod.GET)
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        CodeTableManagerModel model = service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/codetable/view");
        mav.addObject("model", model);
        return mav;
    }

    @RequestMapping(value = "/checkcodetable", method = RequestMethod.GET)
    @ResponseBody
    public int checkcodetable(HttpServletRequest request) {
        String codetable = request.getParameter("codetable");
        try {
            int count = service.checkcodetable(codetable);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}